function Promise(executor) {
    this.promiseState = "pending"
    this.promiseResult = undefined
    this.callbacks = []
    const that = this
    function resolve(data) {
        if (that.promiseState !== "pending") return
        that.promiseResult = data
        that.promiseState = "fulfilled"
        setTimeout(() => {
            that.callbacks.forEach(ele => ele.onResolved(that.promiseResult))
        })
    }
    function reject(data) {
        if (that.promiseState !== "pending") return
        that.promiseResult = data
        that.promiseState = "rejected"
        setTimeout(() => {
            that.callbacks.forEach(ele => ele.onRejected(that.promiseResult))
        })
    }
    try {
        executor(resolve, reject)
    } catch (error) {
        reject(error)
    }
}

Promise.prototype.then = function (onResolved, onRejected) {
    const that = this
    if (typeof onResolved !== "function") {
        onResolved = value => value
    }
    if (typeof onRejected !== "function") {
        onRejected = (error) => {
            throw error
        }
    }
    return new Promise((resolve, reject) => {
        const callback = (type) => {
            try {
                let result = type(this.promiseResult)
                if (result instanceof Promise) {
                    //  应该返回一个状态与结果都和她一致的promise
                    result.then((value) => {
                        resolve(value)
                    }, (error) => {
                        reject(error)
                    })
                } else {
                    //  应该返回状态为【成功】的promise
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }
        }
        if (this.promiseState === 'fulfilled') {
            setTimeout(() => {
                callback(onResolved)
            });
        }
        if (this.promiseState === 'rejected') {
            setTimeout(() => {
                callback(onRejected)
            });
        }
        if (this.promiseState === 'pending') {
            this.callbacks.push({
                onRejected: function () {
                    callback(onRejected)
                },
                onResolved: function () {
                    callback(onResolved)
                },
            })
        }
    })
}

Promise.prototype.catch = function (onRejected) {
    return this.then(undefined, onRejected)
}

Promise.resolve = function (value) {
    return new Promise((resolve, reject) => {
        if (value instanceof Promise) {
            value.then(v => {
                resolve(v)
            }, e => {
                reject(e)
            })
        } else {
            resolve(value)
        }
    })
}

Promise.reject = function (value) {
    return new Promise((resolve, reject) => {
        reject(value)
    })
}

Promise.all = function (promises) {
    return new Promise((resolve, reject) => {
        let count = 0
        let arr = []
        for (let i = 0; i < promises.length; i++) {
            promises[i].then(v => {
                count++;
                arr[i] = v
                if (count === promises.length) {
                    resolve(arr)
                }
            }, r => {
                reject(r)
            })
        }
    })
}

Promise.race = function (promsies) {
    return new Promise((resolve, reject) => {
        for (let i = 0; i < promsies.length; i++) {
            promsies[i].then(v => {
                resolve(v)
            }, r => {
                reject(r)
            })
        }
    })
}